home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
Dots & Pixels
/
headers
/
phaser.h
< prev
next >
Wrap
C/C++ Source or Header
|
1995-09-29
|
2KB
|
65 lines
#pragma once
//
// class phaser is used to implement limited-lifetime. The idea is that
// to implement lifetime of say 10 one has to 'kill off' one tenth of
// the population at every timestep. Example code:
//
// phaser klingon( 10);
// for( int time = 0; time < timesteps_in_simulation)
// {
// (void)klingon.major_step();
// for( int particle_no = 0; particle_no < particles_in_simulation)
// {
// if( klingon.minor_step() == 0)
// {
// kill_particle( the_particles[ particle_no]);
// make_new_particle( the_particles[ particle_no]);
// } else {
// step_particle( the_particles[ particle_no]);
// }
// }
// }
//
// Note: we are using two counters which cycle through 0 ... length - 1
// since any other way will wreak havoc if the number of particles and
// the cycle length have a gcd greater than one (well, I can think of
// ways to get away with only one counter, using a large prime, but
// I'm not 100% sure that that will always work, and it would only
// make the memory requirements sizeof( unsigned int) lower)
//
class phaser
{
public:
phaser( unsigned int length);
unsigned int major_step();
unsigned int minor_step();
const unsigned int cycle_length;
private:
unsigned int major_phase;
unsigned int minor_phase;
};
inline unsigned int phaser::major_step()
{
if( major_phase == 0)
{
major_phase = cycle_length;
}
major_phase -= 1;
minor_phase = major_phase;
return major_phase;
}
inline unsigned int phaser::minor_step()
{
if( minor_phase == 0)
{
minor_phase = cycle_length;
}
minor_phase -= 1;
return minor_phase;
}